import requests
import bs4


r = requests.get("https://search.51job.com/list/090200,000000,0000,40%252C32,9,99,PHP%25E5%25BC%2580%25E5%258F%2591,2,1.html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=")
r.encoding = "GBK"
r.raise_for_status()

bs = bs4.BeautifulSoup(r.text, features='lxml')
# jobs_group = bs.find(name="div", attrs={'id': "resultList", "class": "dw_table"})
# assert isinstance(jobs_group, bs4.element.Tag)

#方法一

# job_items = jobs_group.find_all(name="div", attrs={"class": "el"})
# for item in job_items:
#     if isinstance(item, bs4.element.NavigableString):
#         continue
#     if 'title' in item.attrs['class']:
#         continue
#
#     # print(item.attrs)
#     company = item.p.span.a
#     print(company.string.strip())

# 遍历元素通过find_next
# dw_table = bs.find('div', attrs={"class": "dw_table"})
# item = dw_table.find("div", attrs={"class": ["el", "title"]})
# while True:
#     item = item.find_next("div", attrs={"class": "el"})
#     if not item:
#         break
#     if "title" in item.attrs['class']:
#         continue
#
#     if isinstance(item, bs4.element.NavigableString):
#         continue
#
#     print(item.p.span.a.string.strip())

# 通过children属性找子元素

dw_table = bs.find("div", attrs={"class": "dw_table"})
# children = list(dw_table.children)
# for r in children:
#     if isinstance(r, bs4.element.NavigableString):
#         continue
#     try:
#         print(r.p.span.a.string.strip())
#     except:
#         pass

# 通过contents属性找子元素

# for r in dw_table.contents[13:]:
#     if isinstance(r, bs4.element.NavigableString):
#         continue
#     try:
#         print(r.p.span.a.string.strip())
#     except:
#         print("Invalid node")

# for e in dw_table.contents:
#     if isinstance(r, bs4.element.NavigableString):
#         continue
#
#     job_info = {}
#     try:
#         job_info['title'] = e.p.span.a.string.strip()
#         items = e.find_all('span', recursive=False)
#         job_info['company'] = items[0].a.text
#         job_info['address'] = items[1].text
#         job_info['salary'] = items[2].text
#         print(job_info)
#         # print(len(items))
#     except:
#         pass

# 使用.next_elements获得数据
# title_bar = bs.find("div", attrs={"class": "el title"})
# assert title_bar is not None
# for e in title_bar.next_elements:
#     if isinstance(e, bs4.element.NavigableString):
#         continue
#
#     if "class" not in e.attrs or "el" not in e.attrs['class']:
#         continue
#     job_info = {}
#     # print(e.attrs)
#     try:
#         job_info['title'] = e.p.span.a.string.strip()
#         items = e.find_all('span', recursive=False)
#         job_info['company'] = items[0].a.text
#         job_info['address'] = items[1].text
#         job_info['salary'] = items[2].text
#         print(job_info)
#         # print(len(items))
#     except:
#         pass

# 通过.next_element过的数据
item = bs.find("div", attrs={"class": "el title"})
while item:
    # 指向下一个元素
    item = item.next_element
    if not item:
        continue

    if isinstance(item, bs4.element.NavigableString):
        continue
    job_info = {}
    if "class" in item.attrs and "el" in item.attrs['class']:
        try:
            job_info['title'] = item.p.span.a.string.strip()
            items = item.find_all('span', recursive=False)
            job_info['company'] = items[0].a.text
            job_info['address'] = items[1].text
            job_info['salary'] = items[2].text
            print(job_info)
        except:
            pass



# while True:
#     item = first_item.next_slibing
#     if item is None:
#         break
#     if isinstance(item, bs4.element.NavigableString):
#         continue
#
#     print(item)


